class Solution {
public:
    int maxSubarrayLength(vector<int>& nums, int k) {
        unordered_map<int, int> arr;
        int left = 0;
        int right = 0;
        int res = 0;
        while (right < nums.size())
        {
            arr[nums[right]]++;
            while (arr[nums[right]] > k)
            {
                arr[nums[left]]--;
                left++;
            }
            res = max(res, right - left + 1);
            right++;
        }
        return res;
    }
};